//
//  SystemHelper.m
//  CarCare
//
//  Created by ileo on 14-9-28.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "SystemHelper.h"

#import "NetSystem.h"

#import "MapHelper.h"
#import "UmengMgr.h"
#import "ScreenMgr.h"
#import "RefreshMgr.h"
#import "NotificationHelper.h"

#import "ConfigMgr.h"
#import "NetEngineServer.h"

#import <AVFoundation/AVFoundation.h>

NSString * const APP_WAKE_FROM_BG = @"APP_WAKE_FROM_BG";
@interface SystemHelper()

@property (nonatomic , copy) NSDictionary * updateInfoDic;

@end
@implementation SystemHelper

IMP_SINGLETON

- (instancetype)init
{
    self = [super init];
    if (self) {
        [AISystemInfo sharedInstance];
    }
    return self;
}

- (void)configuration{
    
    [ConfigMgr jsPatchFixBug];

    [MAPMGR startUpdatingLoc];
    [UmengMgr config];

    [NetEngineServer config];

    [NotificationHelper configuration];

    
    __weak __typeof(self) wself = self;
    [ConfigMgr newVersionInfo:^(NSDictionary *dic) {
        wself.updateInfoDic = [NSDictionary dictionaryWithDictionary:dic];
    }];

}
//检测返回更新App的dic
- (NSDictionary *)loadUpdateDic
{
    return self.updateInfoDic;
}
-(void)readAllNotification{
    [[NetSystem readAllNotification] requestSuccess:nil];
}

-(void)clearIconNum{
    //获取应用程序消息通知标记数（即小红圈中的数字）
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    if (badge>0) {
        //如果应用程序消息通知标记数（即小红圈中的数字）大于0，清除标记。
        //清除标记。清除小红圈中数字，小红圈中数字为0，小红圈才会消除。
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    }
}

-(BOOL)allowPushNotification{
    return  [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone;
}

#pragma mark - info

-(BOOL)isFirstTime{
    return [AISystemInfo sharedInstance].isFirstTime;
}

-(BOOL)isOpenMedia{
    AVAuthorizationStatus auth = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    return auth == AVAuthorizationStatusAuthorized;
}

-(void)callPhone:(NSString *)phone{
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

#pragma mark - 右滑返回
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    UIViewController *vc = gestureRecognizer.view.viewController;
    if ([vc isKindOfClass:[UITabBarController class]]) {
        return ((UINavigationController *)((UITabBarController *)vc).selectedViewController).viewControllers.count != 1;
    }else if ([vc isKindOfClass:[UINavigationController class]]){
        return ((UINavigationController *)vc).viewControllers.count != 1;
    }else{
        return NO;
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
@end
